//
//  HLiveMessageView.m
//  linktuIOS
//
//  Created by hejunsong on 17/1/13.
//  Copyright © 2017年 tianxie. All rights reserved.
//

#import "HLiveMessageView.h"
#import "HYYMessageCell.h"
#import "Masonry.h"
#import "HMessageViewModel.h"
#import "HLiveMesssage.h"
@interface HLiveMessageView()<UITableViewDelegate,UITableViewDataSource,HYYMessageCellDelegate>

@property(nonatomic,strong) UITableView *tableView;

@end


@implementation HLiveMessageView


-(instancetype)init
{
    if(self = [super init])
    {
        [self makeUI];
    
    }
    return self;
}

-(instancetype)initWithFrame:(CGRect)frame
{
    if(self = [super initWithFrame:frame])
    {
        [self makeUI];
       
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}
-(void)makeUI
{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.showsVerticalScrollIndicator = NO;
    [self addSubview:_tableView];
    [_tableView registerClass:[HYYMessageCell class] forCellReuseIdentifier:@"messagecellid"];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.mas_left).offset(0);
        make.right.mas_equalTo(self.mas_right).offset(0);
        make.bottom.mas_equalTo(self.mas_bottom).offset(0);
        make.top.mas_equalTo(self.mas_top).offset(0);
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.messageViewModel.mesageArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HYYMessageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"messagecellid"];
    cell.delegate = self;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.messgae = self.messageViewModel.mesageArray[indexPath.row];
//    cell.message1 = self.messageViewModel.mesageArray[indexPath.row];
    return cell;
}
#pragma mark UITableViewDelgate
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HLiveMesssage *message = self.messageViewModel.mesageArray[indexPath.row];
    return message.layout.textBoundingSize.height;
//    return 30;
    
}

-(void)hyyMessageCellWithuserID:(LTUser *)user
{
    if(self.delegate && [self.delegate respondsToSelector:@selector(hliveMessageViewWithuserID:)])
    {
        [self.delegate hliveMessageViewWithuserID:user];
    }

}

-(void)reloadData
{
    [self.tableView reloadData];
    if([self.messageViewModel.mesageArray count] > 1)
    {
        [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:[self.messageViewModel.mesageArray count] - 1 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
    }
}
-(void)dealloc
{
    NSLog(@"dealloc --HLiveMessageView");
    
}

@end
